Explicit Resource Management for pg pool client#3661
Conversation
bac8c73 to
7d69534
Compare
|
Hmm, looks like eslint needs to be upgraded before it'll parse the |
7d69534 to
0cb5f67
Compare
0cb5f67 to
24caf46
Compare
There was a problem hiding this comment.
Not sure about destroyOnDispose, i.e. whether its job is common enough to justify it and whether it’s the best API for the job. Would suggest initializing to false if kept. Note ; I recognize that it doesn’t satisfy the use case you mentioned. Maybe release the feature without that API initially, since it can be added easily?.release(true) continues to work for error cases given that double release is a no-op
Edit: Sorry, I don’t know where I got that from – we haven’t even done PooledClient yet.
|
|
||
| client.release = this._releaseOnce(client, idleListener) | ||
|
|
||
| if (Symbol.dispose) { |
There was a problem hiding this comment.
I think this would normally go on the prototype, but pg-pool’s approach is already a bit messy (it’s lacking a PooledClient object that’s specific to one acquisition operation) so it’s also not the end of the world if the patch goes in as-is.
There was a problem hiding this comment.
Agreed, and that's why I implemented it like this.
@types/pg has a PooledClient type with the release signature, and I intend to make a PR there to add the [Symbol.dispose] addition later.
Yeah I wasn't sure about the API either but I couldn't think of a better way to do it. Maybe a That said, unfortunately I don't think relying on double-release is workable either, because the second release, which will be called from within |
36ef0cf to
8c8d974
Compare
|
I got here because I'm implementing dispose on a client wrapper. One question, why implement using client = await pool.connect({ destroyOnDispose: true });Maybe I'm not understanding the use case you mention. |
|
@olsonpm That does work for my usecase, I just didn't think of it. But on the other hand it's still a shift from how late you can make the decision not to return the client to the pool -- with An example where it might be desirable to destroy the client rather than release it back to the pool would be if you made a |
|
Ah thanks for that use case Hyperair. I was aware the decision would change the semantics as you mentioned, but couldn't think of why you would want that flexibility. |
When `Symbol.dispose` is defined, define a disposer function that simply calls `this.release()`. This lets the `PoolClient` with the `using` syntax work with any downstream-overridden `release` functions. This makes `PoolClient` support Explicit Resource Management when the runtime supports it. Fixes: brianc#3515
Add a `destroyOnDispose` property on the client to signal that `[Symbol.dispose]()` should call `client.release(true)` instead of `client.release()`.
Co-authored-by: Charmander <~@charmander.me>
`connect` has two `n`s, and we don't like semicolons Co-authored-by: Charmander <~@charmander.me>
8c8d974 to
f56d448
Compare
|
Hey thanks for this! I actually set out to do this a while ago and due to the linting and it not working on older versions of node I grew frustrated and thought I'd do it later. And now it's done! I'm also not sold on One possible alternative is to just allow ended clients to be returned to the pool (via the disposal or via using client = await pool.connect()
try {
client.query("EXPLODE!")
} catch (e) {
client.end()
throw e // (or just log it if you want or whatever)
}Thoughts on that? |
Add support for Explicit Resource Management for
pg.Poolclients.This is implemented using
[Symbol.dispose]instead of[Symbol.asyncDispose]becauseclient.release()is a synchronous function, not async.Additionally, add a second property
client.destroyOnDisposeso thatSymbol.disposesupports callingclient.release(true). This is useful for a usecase that changes connection parameters in a way that is troublesome to reset upon release to the pool, e.g.Fixes: #3515
Note: This PR has been rebased on top of #3662, so there are some irrelevant commits until that PR is merged.